﻿
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Core;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.IO;

namespace Yuanlin.Business
{
    public class ProductSpecificationEditControl : YuanlinUserControl,
        IReplyConverter<ProductSpecificationReply, GetOperation>
    {
        protected ValuedTextBox titleValuedTextBox;
        protected ValuedTextBox companyValuedTextBox;
        protected IButton SaveButton;
        protected IButton BackButton;
        private ProductSpecification productSpecification;
        private int productCategoryId = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.ProductCategoryId);

        public override void GetRequests(List<BaseRequestBinder> requestBinders)
        {
            requestBinders.Add(RequestHelper.BuildGetProductSpecificationRequest(this));
        }
        protected override void OnLoad(EventArgs e)
        {
            SaveButton.Click += new EventHandler(SaveButton_Click);
            BackButton.Click += new EventHandler(BackButton_Click);
        }
        private void Bind()
        {
            if(productSpecification!=null)
            {
                this.titleValuedTextBox.Value = productSpecification.Title.Value;
                this.companyValuedTextBox.Value = productSpecification.Unit.Value;
            }   
        }
        public void BackButton_Click(object sender, EventArgs e)
        {
            PageRedirect();
        }
        public void SaveButton_Click(object sender, EventArgs e)
        {
            if (productSpecification != null)
            {
                productSpecification.Title.Value =  CensorWordManager.CensorWord(this.titleValuedTextBox.Value);
                productSpecification.Unit.Value =  CensorWordManager.CensorWord(this.companyValuedTextBox.Value);

                if (BusinessManager.UpdateProductSpecification(productSpecification) == -2)
                {
                    Globals.ShowMessage(this.Page, "修改失败", "此信息已经存在！");
                    return;
                }
            }
            PageRedirect();
        }
        private void PageRedirect()
        {
            Page.Response.Redirect(SiteUrls.Instance().GetProductSpecificationListUrl(productCategoryId));
        }

        #region IReplyConverter<ProductSpecificationReply,GetOperation> Members

        public void ConvertReply(ProductSpecificationReply reply, GetOperation operation)
        {
            if (reply != null)
            {
                productSpecification = reply.Entity as ProductSpecification;

                if (!Page.IsPostBack)
                {
                    Bind();
                }
            }
        }

        #endregion
    }
}